package com.jcark.arch.net.utils

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonParser
import com.google.gson.JsonPrimitive
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type
import java.util.TreeMap

/**
 * Description:GSON解析工具类 <br>
 * @author King.Zi<br>
 * @date 2025/03/03<br>
 * Email: 78688689@qq.com
 */
object GsonParserUtils {
    private val gson: Gson by lazy {
        GsonBuilder()
            .registerTypeAdapter(object :
                TypeToken<TreeMap<String, Any>>() {}.type,
                object : JsonDeserializer<TreeMap<String, Any>> {
                    override fun deserialize(
                        json: JsonElement,
                        typeOfT: Type,
                        context: JsonDeserializationContext?
                    ): TreeMap<String, Any> {
                        var treeMap = TreeMap<String, Any>()
                        val jsonObject = json.asJsonObject
                        val entrySet = jsonObject.entrySet()
                        entrySet.forEach {
                            val any = it.value
                            if (any is JsonPrimitive) {
                                treeMap[it.key] = any.asString
                            } else {
                                treeMap[it.key] = any
                            }
                        }
                        return treeMap

                    }
                }).create()
    }

    /**
     * 转成json
     *
     * @param any
     * @return
     */
    fun toJson(any: Any) = gson.toJson(any)


    /**
     * 转成bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    fun <T> toBean(gsonString: String, cls: Class<T>): T {
        return gson.fromJson(gsonString, cls)
    }

    /**
     * 转成list
     *
     * @param gsonString
     * @param cls
     * @return
     */
    fun <T> toList(gsonString: String, cls: Class<T>): ArrayList<T> {
        val mList = ArrayList<T>()
        val array = JsonParser().parse(gsonString).asJsonArray
        for (elem in array) {
            mList.add(gson.fromJson(elem, cls))
        }
        return mList
    }

    /**
     * 转成list中有map的
     *
     * @param gsonString
     * @return
     */
    fun <T> toMapList(gsonString: String): List<Map<String, T>> {
        var list: List<Map<String, T>> = arrayListOf()
        if (gson != null) {
            list = gson.fromJson(
                gsonString,
                object : TypeToken<List<Map<String, T>>>() {}.type
            )
        }
        return list
    }

    /**
     * 转成TreeMap
     *
     * @param gsonString
     * @return
     */
    fun toTreeMap(gsonString: String): TreeMap<String, Any> {
        var map: TreeMap<String, Any> = TreeMap()
        if (gson != null) {
            val typeToken = object : TypeToken<TreeMap<String, Any>>() {}.type
            map = gson.fromJson(gsonString, typeToken)
        }
        return map
    }

    /**
     * 转成map
     */
    fun toHashMap(any: Any): HashMap<String,Any> {
        val json = toJson(any)
        var map: HashMap<String, Any> = hashMapOf()
        if (gson != null) {
            val typeToken = object : TypeToken<HashMap<String, String>>() {}.type
            map = gson.fromJson(json, typeToken)
        }
        return map
    }
}